﻿using CleanArchitecture.Infrastructure.Entities.Settings;
using FluentValidation;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using System.Linq;
using System.Text;

namespace CleanArchitecture.Web.Validators
{
    /// <summary>
    /// Validator extensions
    /// </summary>
    public static class ValidatorExtensions
    {
        /// <summary>
        /// Set decimal validator
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="ruleBuilder">RuleBuilder</param>
        /// <param name="maxValue">Maximum value</param>
        /// <returns>Result</returns>
        public static IRuleBuilderOptions<T, decimal> IsDecimal<T>(this IRuleBuilder<T, decimal> ruleBuilder, decimal maxValue)
        {
            return ruleBuilder.SetValidator(new DecimalPropertyValidator(maxValue));
        }

        /// <summary>
        /// Set username validator
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="ruleBuilder">RuleBuilder</param>
        /// <param name="adminuserSetting">AdminUserSettings</param>
        /// <returns>Result</returns>
        public static IRuleBuilderOptions<T, string> IsLoginUsername<T>(this IRuleBuilder<T, string> ruleBuilder, AdminUserSettings adminuserSettings)
        {
            return ruleBuilder.SetValidator(new AdminUserLoginNamePropertyValidator(adminuserSettings));
        }

        public static IRuleBuilderOptions<T, string> IsLoginCustomername<T>(this IRuleBuilder<T, string> ruleBuilder, CustomerSettings customerSettings)
        {
            return ruleBuilder.SetValidator(new CustomerLoginNamePropertyValidator(customerSettings));
        }
        /// <summary>
        /// Set password validator
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="ruleBuilder">RuleBuilder</param>
        /// <param name="adminuserSetting">AdminUserSettings</param>
        /// <returns>Result</returns>
        public static IRuleBuilderOptions<T, string> IsLoginPasswordCorrect<T>(this IRuleBuilder<T, string> ruleBuilder, int passwordminLen)
        {
            return ruleBuilder.SetValidator(new AdminUserPasswordPropertyValidator(passwordminLen));
        }

        public static IRuleBuilderOptions<T, string> IsCustomerLoginPasswordCorrect<T>(this IRuleBuilder<T, string> ruleBuilder, int passwordminLen)
        {
            return ruleBuilder.SetValidator(new CustomerPasswordPropertyValidator(passwordminLen));
        }
        /// <summary>
        /// Set Mobile Number validator
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="ruleBuilder">RuleBuilder</param>
        /// <returns>Result</returns>
        public static IRuleBuilderOptions<T, string> IsValidMobile<T>(this IRuleBuilder<T, string> ruleBuilder)
        {
            return ruleBuilder.SetValidator(new MobileValidator());
        }

        // <summary>
        /// 获取验证消息提示并格式化提示
        /// </summary>
        public static string GetValidationSummary(this ModelStateDictionary modelState, string separator = "\r\n")
        {
            if (modelState.IsValid) return null;

            var error = new StringBuilder();

            foreach (var item in modelState)
            {
                var state = item.Value;
                var message = state.Errors.FirstOrDefault(p => !string.IsNullOrWhiteSpace(p.ErrorMessage))?.ErrorMessage;
                if (string.IsNullOrWhiteSpace(message))
                {
                    message = state.Errors.FirstOrDefault(o => o.Exception != null)?.Exception.Message;
                }
                if (string.IsNullOrWhiteSpace(message)) continue;

                if (error.Length > 0)
                {
                    error.Append(separator);
                }

                error.Append(message);
            }

            return error.ToString();
        }
    }
}
